Search Insert Position
May 1, 2016
Simply iterating through the array and accounting for test cases.
Full Solution in Java:
public class Solution { public int searchInsert(int[] nums, int target) { if(target< nums[0]){ return 0; } if(target>nums[nums.length-1]){ return nums.length; } if(nums[nums.length-1]==target){ return nums.length-1; } for(int i=0; i< nums.length-1; i++){ if(nums[i]==target){ return i; } if(target>nums[i] && target< nums[i+1]){ return i+1; } } return 22;//or any other random number } }